<?php
namespace app\hsycms\controller;
use think\Controller;
use think\Db;
use app\hsycms\controller\Base;

class School extends Base
{
    public function school(){
        $list = db('school')->order('id','desc')->where(function($query){
            $school_name = input('school_name');
            if(!empty($school_name)){
                $query->where('school_name','like','%'.$school_name.'%');
            }
        })->paginate(10,false,['query' => request()->param()]);
        $page = $list->render();		
        $this->assign("list",$list);
        $this->assign("page",$page);
        return $this->fetch(); 
    }
    //添加/修改
    public function addschool(){
        $id = input('id');
		if($_POST){
			$m = db('school');
            $data=input("post.");
            $data['label'] = implode(',',$data['label']);	
            $data['gradation'] = implode(',',$data['gradation']);	
            $data['level'] = implode(',',$data['level']);		
            if($id){//更新
                if($m->where('id',$id)->update($data)!==false)
				return json(['status'=>'y','info'=>'修改成功']);
            }else{//添加
                $data['creat_time'] = date('Y-m-d H:i:s',time());
                $id = $m->insertGetId($data);
                return json(['status'=>'y','info'=>'添加成功']);
            }			
		}else{
			if($id){
                $info = db('school')->where('id',$id)->find();
                $info['label'] = explode(',',$info['label']);	
                $info['level'] = explode(',',$info['level']);	
				$this->assign("info",$info);
            }
            $label = db('label')->order('id','desc')->select();
            $this->assign("label",$label);
			return $this->fetch();
		}
    }
    //删除
    public function delschool(){
        $id  = input('id');						
		if(db('school')->where('id',$id)->delete()){
            return json(['status'=>'y','info'=>'删除成功']);			
        }else{
            return json(['status'=>'n','info'=>'删除失败']);			
        }	
        return json(['status'=>'y','info'=>'删除成功']);		
    }
    //专业列表
    public function professional(){
        $list = db('gradation')->alias('a')->order('a.id','desc')->join('school b','a.school_id = b.id')->where(function($query){
            $school_id = input("school_id");
            if(!empty($school_id)){
                $query->where('school_id',$school_id);
            }
            $status = input("status");
            if(!empty($status)){
                $query->where('status',$status);
            }
        })->field('a.id,a.professional_title,a.level,a.creat_time,a.year,a.hot,b.school_name,b.type')->paginate(10,false,['query' => request()->param()]);
        $page = $list->render();
        $this->assign("list",$list);
        $this->assign("school",db('school')->field('id,school_name')->select());
        $this->assign("page",$page);
        return $this->fetch();
    }
    //添加/编辑专业
    public function addprofessional(){
        $id = input('id');
        if($_POST){
            $m = db('gradation');
            $data=input("post.");	
            if($id){
                if($m->where('id',$id)->update($data)!==false)
				return json(['status'=>'y','info'=>'修改成功']);
            }else{//添加
                
                $data['creat_time'] = date('Y-m-d H:i:s',time());
                $id = $m->insertGetId($data);
                return json(['status'=>'y','info'=>'添加成功']);
            }
        }else{
          
            $info = db('gradation')->where('id',$id)->find();
            $this->assign("school",db('school')->field('id,school_name')->select());
            $this->assign("info",$info);
            return $this->fetch();
        }
    }
    //删除专业
    public function delprofessional(){
        $id  = input('id');						
		if(db('gradation')->where('id',$id)->delete()){
            return json(['status'=>'y','info'=>'删除成功']);			
        }else{
            return json(['status'=>'n','info'=>'删除失败']);			
        }	
        return json(['status'=>'y','info'=>'删除成功']);		
    }
    
    public function classification(){
        $list = db('type')->order('id','desc')->paginate(10,false,['query' => request()->param()]);
        $page = $list->render();		
        $this->assign("list",$list);
        $this->assign("page",$page);
        return $this->fetch();
    }
    //添加/编辑分类
    public function addclassification(){
        $id = input('id');
        $m = db('type');
        if($_POST){
            $data=input("post.");
            if($id){
                if($m->where('id',$id)->update($data)!==false)
				return json(['status'=>'y','info'=>'修改成功']);
            }else{//添加
                $data['creat_time'] = date('Y-m-d H:i:s',time());
                $id = $m->insertGetId($data);
                return json(['status'=>'y','info'=>'添加成功']);
            }	
        }else{
            $info = db('type')->where('id',$id)->find();
            $this->assign("list",$info);
            $this->assign("info",$info);  
        }
        return $this->fetch();
    }
    //删除分类
    public function delclassification(){
        $id  = input('id');						
		if(db('type')->where('id',$id)->delete()){
            return json(['status'=>'y','info'=>'删除成功']);			
        }else{
            return json(['status'=>'n','info'=>'删除失败']);			
        }	
        return json(['status'=>'y','info'=>'删除成功']);
        return json(['status'=>'y','info'=>'删除成功']);
    }
    //标签列表
    public function label(){
        $list = db('label')->order('id','desc')->paginate(10,false,['query' => request()->param()]);
        $page = $list->render();		
        $this->assign("list",$list);
        $this->assign("page",$page);
        return $this->fetch();
    }
    //添加/编辑标签
    public function addlabel(){
        $id = input('id');
        if($_POST){
            $data=input("post.");
            $m = db('label');
            if($id){
                if($m->where('id',$id)->update($data)!==false)
				return json(['status'=>'y','info'=>'修改成功']);
            }else{
                $data['creat_time'] = date('Y-m-d H:i:s',time());
                $id = $m->insertGetId($data);
                return json(['status'=>'y','info'=>'添加成功']);
            }
        }else{
            $info = db('label')->where('id',$id)->find();
            $this->assign("info",$info);  
        }
        return $this->fetch();
    }
    //删除标签
    public function dellabel(){
        $id  = input('id');						
		if(db('label')->where('id',$id)->delete()){
            return json(['status'=>'y','info'=>'删除成功']);			
        }else{
            return json(['status'=>'n','info'=>'删除失败']);			
        }	
    }
}